package com.bird.ecmainmenu3.util;

import android.content.ComponentName;
import android.content.Context;
import android.content.Intent;
import android.content.SharedPreferences;
import android.util.Log;
import android.widget.ImageView;
import android.widget.Toast;

import androidx.annotation.NonNull;

import java.util.List;
import java.util.Map;

public final class Utils {
    public static final String TAG = "XbsLauncher_v1.0.7->";

    public static final String CATEGORY_MAIN_MENU_BIRD = "bird.category.MAIN_MENU";

    public static final String PHONE_PKG = "com.bird.ecbluetooth";
    public static final String PHONE_CLS = "com.bird.ecbluetooth.ui.PhoneActivity";

    public static final String KUWO_PKG = "cn.kuwo.kwmusiccar";
    public static final String KUWO_CLS = "cn.kuwo.kwmusiccar.ui.WelcomeActivity";

    public static final String DVR_PKG = "com.bird.dvrclient";
    public static final String DVR_CLS = "com.bird.dvrclient.ui.activity.DvrActivity";

    public static final String AUTONAVI_PKG = "com.autonavi.amapauto";
    public static final String AUTONAVI_CLS = "com.autonavi.auto.remote.fill.UsbFillActivity";

    public static final String CONNECTION_PKG = "net.easyconn";
    public static final String CONNECTION_CLS = "net.easyconn.ui.ExpMainActivity";

    public static final String SETTINGS_PKG = "com.bird.carsettings";
    public static final String SETTINGS_CLS = "com.bird.carsettings.SettingsActivity";

    public static final String GALLERY_PKG = "com.bird.ecgallery";
    public static final String GALLERY_CLS = "com.bird.ecgallery.photo.PhotoActivity";

    public static final String DEBUGER_PKG = "com.bird.birddebuger";
    public static final String DEBUGER_CLS = "com.bird.birddebuger.DebugActivity";

    public static final String MEDIA_PKG = "com.bird.ecmultimedia";
    public static final String MEDIA_CLS = "com.bird.ecmultimedia.MultimediaActivity";

    public static final String WEATHER_PKG = "com.xbs.weather";
    public static final String WEATHER_CLS = "com.xbs.weather.WeatherActivity";

    public static final String UPGRADE_PKG = "com.bird.upgrade";
    public static final String UPGRADE_CLS = "com.bird.upgrade.activity.MPUUpgradeActivity";

    public static final String DOFUN_PKG = "com.dofun.market";
    public static final String DOFUN_CLS = "com.auto.market.module.launch.LaunchActivity";

    public static final String RGB_PKG = "wl.smartled.rgb";
    public static final String RGB_CLS = "com.easylink.colorful.activity.LaunchActivity";

    public static final String QIYI_PKG = "com.arcvideo.car.video";
    public static final String QIYI_CLS = "com.arcvideo.car.video.SplashActivity";

    public static final String AIR_PKG = "com.bird.ecairconditioner";
    public static final String BACKUP_PKG = "com.bird.backup";

    public static final String AWEME_PKG = "com.ss.android.ugc.aweme";
    public static final String AWEME_CLS = "com.ss.android.ugc.aweme.carplay.main.CarPlayMainActivity";

    public static final String BILI_PKG = "com.bilibili.bilithings";
    public static final String BILI_CLS = "com.bilibili.bilithings.splash.SplashActivity";

    public static final String SAGA_PKG = "com.king.candycrushsaga";
    public static final String SAGA_CLS = "com.king.candycrushsaga.CandyCrushSagaActivity";

    public static final String CMUSCI_PKG = "com.netease.cloudmusic.iot";
    public static final String CMUSCI_CLS = "com.netease.cloudmusic.iot.app.LoadingActivity";

    public static final String MOKU_PKG = "uk.co.aifactory.gomokufree";
    public static final String MOKU_CLS = "org.cocos2dx.cpp.AppActivity";

    public static final String KUGOU_PKG = "com.kugou.android.auto";
    public static final String KUGOU_CLS = "com.kugou.android.auto.ui.activity.main.MediaActivity";

    public static final String QMUSIC_PKG = "com.tencent.qqmusiccar";
    public static final String QMUSIC_CLS = "com.tencent.qqmusiccar.app.activity.AppStarterActivity";

    public static final String SNAKE_PKG = "com.arcade.freesnake";
    public static final String SNAKE_CLS = "com.arcade.freesnake.WelActivity";

    public static final String NEWS_PKG = "com.ss.android.article.news";
    public static final String NEWS_CLS = "com.ss.android.article.news.activity.MainActivity";

    public static final String XC_PKG = "com.directm.platform.stackcolors.xc";
    public static final String XC_CLS = "com.was.api.WasActivity";

    public static final String XI_PKG = "com.ximalaya.ting.android.car";
    public static final String XI_CLS = "com.ximalaya.ting.android.car.business.module.home.MainActivity";

    public static final String FJ_PKG = "com.halfbrick.fruitninjafree";
    public static final String FJ_CLS = "com.halfbrick.mortar.MortarGameLauncherActivity";

    public static final Map<String, String> APP_MAP = Map.ofEntries(
            Map.entry(PHONE_PKG, PHONE_CLS),
            Map.entry(KUWO_PKG, KUWO_CLS),
            Map.entry(DVR_PKG, DVR_CLS),
            Map.entry(AUTONAVI_PKG, AUTONAVI_CLS),
            Map.entry(CONNECTION_PKG, CONNECTION_CLS),
            Map.entry(SETTINGS_PKG, SETTINGS_CLS),
            Map.entry(GALLERY_PKG, GALLERY_CLS),
            Map.entry(DEBUGER_PKG, DEBUGER_CLS),
            Map.entry(MEDIA_PKG, MEDIA_CLS),
            Map.entry(WEATHER_PKG, WEATHER_CLS),
            Map.entry(DOFUN_PKG, DOFUN_CLS),
            Map.entry(RGB_PKG, RGB_CLS),
            Map.entry(QIYI_PKG, QIYI_CLS),
            Map.entry(AWEME_PKG, AWEME_CLS),
            Map.entry(BILI_PKG, BILI_CLS),
            Map.entry(SAGA_PKG, SAGA_CLS),
            Map.entry(CMUSCI_PKG, CMUSCI_CLS),
            Map.entry(MOKU_PKG, MOKU_CLS),
            Map.entry(KUGOU_PKG, KUGOU_CLS),
            Map.entry(QMUSIC_PKG, QMUSIC_CLS),
            Map.entry(SNAKE_PKG, SNAKE_CLS),
            Map.entry(NEWS_PKG, NEWS_CLS),
            Map.entry(XC_PKG, XC_CLS),
            Map.entry(XI_PKG, XI_CLS),
            Map.entry(FJ_PKG, FJ_CLS)
    );

    public static final List<String> APP_LIST = List.of(
            AIR_PKG,
            PHONE_PKG,
            KUWO_PKG,
            DVR_PKG,
            AUTONAVI_PKG,
            CONNECTION_PKG,
            SETTINGS_PKG,
            GALLERY_PKG,
            DEBUGER_PKG,
            MEDIA_PKG,
            WEATHER_PKG,
            DOFUN_PKG,
            RGB_PKG,
            QIYI_PKG,
            AWEME_PKG,
            BILI_PKG,
            SAGA_PKG,
            CMUSCI_PKG,
            MOKU_PKG,
            KUGOU_PKG,
            QMUSIC_PKG,
            SNAKE_PKG,
            NEWS_PKG,
            XC_PKG,
            XI_PKG,
            FJ_PKG
    );

    public static void backHome(@NonNull Context context) {
        Log.d(TAG, "backHome: ");
        try {
            Intent intent = new Intent();
            intent.setAction(Intent.ACTION_MAIN);
            intent.addCategory(Intent.CATEGORY_HOME);
            intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
            context.startActivity(intent);
        } catch (Exception e) {
            Log.e(TAG, "backHome: ", e);
        }
    }

    public static void startApp(@NonNull Context context,@NonNull String pkg, @NonNull String cls) {
        Log.d(TAG, "startApp: pkg=" + pkg + " cls=" + cls);
        try {
            if (pkg.isEmpty()) {
                Toast.makeText(context, "目标应用程序不存在", Toast.LENGTH_SHORT).show();
            } else if (cls.isEmpty()) {
                context.getPackageManager().getLaunchIntentForPackage(pkg);
            } else {
                Intent intent = new Intent();
                intent.setComponent(new ComponentName(pkg, cls));
                intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
                context.startActivity(intent);
            }
        } catch (Exception e) {
            Log.e(TAG, "startApp: ", e);
        }
    }

    public static void saveAppData(@NonNull Context context,@NonNull String key, @NonNull String value)  {
        SharedPreferences sharedPreferences = context.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.putString(key, value);
        editor.apply();
    }

    public static String getAppData(@NonNull Context context,@NonNull String key)  {
        SharedPreferences sharedPreferences = context.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
        return sharedPreferences.getString(key, "");
    }

    public static void removeAppData(@NonNull Context context,@NonNull String key)  {
        SharedPreferences sharedPreferences = context.getSharedPreferences("my_prefs", Context.MODE_PRIVATE);
        SharedPreferences.Editor editor = sharedPreferences.edit();
        editor.remove(key);
        editor.apply();
    }
}
